package com.servlet;

import com.pojo.Supplier_info;
import com.service.Supplier_infoService;
import com.service.impl.Supplier_infoServiceImpl;
import com.util.PageUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Supplier_infoServlet", value = "/Supplier_infoServlet")
public class Supplier_infoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String tag = request.getParameter("tag");
        int pageIndex = request.getParameter("pageIndex") == null ? 1 : Integer.parseInt(request.getParameter("pageIndex"));
        int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request.getParameter("pageSize"));
        String search = request.getParameter("search") == null ? "" : request.getParameter("search");
        Supplier_infoService supplierInfoService = new Supplier_infoServiceImpl();
        PrintWriter writer = response.getWriter();
        if ("edit".equals(tag)) {
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            Supplier_info supplier = supplierInfoService.findSupplierById(supplierId);
            String supplierJsonStr = JSON.toJSONString(supplier);
            writer.write(supplierJsonStr);
            writer.close();
        } else if ("del".equals(tag)) {
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            int delFlag = supplierInfoService.delSupplier(supplierId);
            writer.print(delFlag > 0);
            writer.close();
        } else {
            PageUtil<Supplier_info> pageSupplierList = supplierInfoService.findPageSupplierList(pageIndex, pageSize, search);
            String pageSupplierListJsonStr = JSON.toJSONString(pageSupplierList);
            writer.write(pageSupplierListJsonStr);
            writer.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String tag = request.getParameter("tag");
        String name = request.getParameter("name");
        String contactPerson = request.getParameter("contactPerson");
        String contactNumber = request.getParameter("contactNumber");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        int status = Integer.parseInt(request.getParameter("status"));
        Supplier_info supplierInfo = new Supplier_info(0, name, contactPerson, contactNumber, email, address, status);
        Supplier_infoService supplierInfoService = new Supplier_infoServiceImpl();
        PrintWriter writer = response.getWriter();
        if ("addSave".equals(tag)) {
            int addFlag = supplierInfoService.addSupplier(supplierInfo);
            writer.print(addFlag > 0);
            writer.close();
        } else if ("editSave".equals(tag)) {
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            supplierInfo.setSupplierId(supplierId);
            int editFlag = supplierInfoService.updateSupplier(supplierInfo);
            writer.print(editFlag > 0);
            writer.close();
        }
    }
}
